package com.hqj.utils;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * IP工具类
 */
public class IpUtils {

    /**
     * 获取ip地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request){
        if(request==null){
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if(StringUtils.isEmpty(ip) || "unknown".equals(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(StringUtils.isEmpty(ip) || "unknown".equals(ip)){
            ip = request.getHeader("X-Forwarded-For");
        }
        if(StringUtils.isEmpty(ip) || "unknown".equals(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(StringUtils.isEmpty(ip) || "unknown".equals(ip)){
            ip = request.getHeader("X-Real-IP");
        }
        if(StringUtils.isEmpty(ip) || "unknown".equals(ip)){
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip)?"127.0.0.1":ip;
    }
}
